package com.mxx.common.data.utils;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

/**
 * @Author itzgyw
 * @Date 2020-01-09
 * @Time 13:20
 * @Description:
 */
public class PageUtil {
    public static final Sort DEFAULT_SORT = Sort.by(Sort.Direction.DESC, "createTime");

    public static Sort sortDesc(String... columns) {
        return Sort.by(Sort.Direction.DESC, columns);
    }

    public static Sort sortAsc(String... columns) {
        return Sort.by(Sort.Direction.ASC, columns);
    }

    public static Sort unSorted() {
        return Sort.unsorted();
    }

    public static SortPaged sortPagedDESC(String... columns) {
        return SortPaged.create(sortDesc(columns));
    }

    public static SortPaged sortPagedASC(String... columns) {
        return SortPaged.create(sortAsc(columns));
    }

    public static SortPaged unSortPaged() {
        return SortPaged.unSort;
    }

    public static Pageable createPage(Integer page, Integer size) {
        return PageRequest.of(page, size);
    }

    public static Pageable createPage(Integer page, Integer size, Sort sort) {
        return PageRequest.of(page, size, sort);
    }

    public static Pageable unPaged() {
        return Pageable.unpaged();
    }


    public static Pageable createDefaultPage(Integer page, Integer size) {
        return PageRequest.of(defaultPage(page), defaultSize(size));
    }

    public static Pageable createDefaultPage(Integer page, Integer size, Sort sort) {
        return PageRequest.of(defaultPage(page), defaultSize(size), sort);
    }


    public static int defaultPage(Integer page) {
        return page == null ? 0 : page;
    }

    public static int defaultSize(Integer size) {
        return size == null || size == 0 ? 15 : size;
    }

    public static int defaultPage(Integer page, Integer defaultPage) {
        return page == null || page < 0 ? defaultPage : page;
    }

    public static int defaultSize(Integer size, Integer defaultSize) {
        return size == null || size <= 0 ? defaultSize : size;
    }
}
